//
//  VisitorLoginView.swift
//  SinaWeiBo
//
//  Created by gxiangzi on 15/9/1.
//  Copyright © 2015年 hqu. All rights reserved.
//

import UIKit

class VisitorLoginView: UIView {
    
    /// 手码的时候，调用的方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    /// 使用xib 或者storyBoard的时候调用
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
    
    
     // MARK: - 设置 UI 信息
    func setInfo(imageName: String?, labelText:String? ) {
        // 如果有图片的话，就说明，不是首页，是其他页
        if let imgName:String = imageName {
            homeView.image = UIImage(named: imgName)
            labelView.text = labelText
            iconView.hidden = true
        }
        else {
            startAniation()
        }
    }
    
    private func startAniation () {
     
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.duration = 2
        anim.repeatCount = MAXFLOAT
        
        // 设置动画不删除
        anim.removedOnCompletion = false
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    private func setupUI() {
        
        // 1. 添加控件(控件用懒加载)
        addSubview(iconView)
        addSubview(mask)
        addSubview(homeView)
        addSubview(labelView)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 2. 设置自动布局(原生的布局方式)
        // view1.attr1 = view2.attr2 * multiplier + constant
        
        // 1> 设置图片
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))

        // 2> 设置小房子
        homeView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        
        // 3> 设置文字描述(文本不需要设置)
        labelView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: labelView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: labelView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: labelView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        
        // 4> 设置注册按钮
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: labelView, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: labelView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant:100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 30))
        
        // 5> 设置登陆按钮
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: labelView, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: labelView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 30))
        
        // 设置mask
        mask.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[v]-(-35)-[regButton]", options: [], metrics: nil, views: ["v":mask,"regButton":registerButton]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[v]-0-|", options: [], metrics: nil, views: ["v":mask]))
        
        backgroundColor = UIColor(white: 237.0/255, alpha: 1.0)
    }

     // MARK: - 懒加载的方式添加 UI 控件
    private lazy var mask: UIImageView = {
    
        let mask = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return mask
    }()
    
    private lazy var iconView: UIImageView = {
    
        let iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iconView
    }()
    
    private lazy var  homeView:UIImageView = {
        let homeView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return homeView
    }()
    
    private lazy var labelView : UILabel = {
        let label = UILabel()
        
        label.text = " 关注一些人，会这里看看会有什么惊喜关注一些人，会这里看看会有什么惊喜"
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.darkGrayColor()
       label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.Center
        return label
    }()
    
     lazy var  registerButton: UIButton = {
        
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        return btn
        
    }()
    
     lazy var loginButton:UIButton = {
        
        let btn = UIButton(type: UIButtonType.Custom)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("登陆", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        return btn
    }()
      

}
